package com.ns.scs.hots.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ns.basic.dict.mapper.DictItemEntryMapper;
import com.ns.basic.dict.vo.DictItemEntryVO;
import com.ns.core.enums.GeneralEnum;
import com.ns.core.utils.date.CalendarPattern;
import com.ns.core.utils.date.CalenderUtil;
import com.ns.scs.hots.entry.HotsEntry;
import com.ns.scs.hots.mapper.HotsHistoryMapper;
import com.ns.scs.hots.mapper.HotsMapper;
import com.ns.scs.hots.vo.HotsGroupVO;
import com.ns.scs.hots.vo.HotsTrendVO;
import com.ns.scs.hots.vo.HotsVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 热搜top10业务实现
 *
 * @author lw
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class HotsServiceImpl extends ServiceImpl<HotsMapper, HotsEntry> implements IHotsService {

    private final HotsMapper mapper;

    private final DictItemEntryMapper dictItemEntryMapper;

    private final HotsHistoryMapper historyMapper;


    @Override
    public List<HotsGroupVO> getGroupHots() {
        List<HotsGroupVO> hotsGroup = new ArrayList<>();

        // 查询数据,分组数据
        List<HotsVO> hotsList = mapper.selectGroupHots();
        if (CollectionUtils.isEmpty(hotsList)) {
            return Collections.emptyList();
        }
        // 数据分组
        Map<String, List<HotsVO>> maps = new HashMap<>();
        hotsList.forEach(hot -> {
            List<HotsVO> sourceHots = maps.getOrDefault(hot.getSource(), new ArrayList<>());
            sourceHots.add(hot);
            maps.put(hot.getSource(), sourceHots);
        });

        // 获取字典数据
        List<DictItemEntryVO> items = dictItemEntryMapper.selectByDictCode(GeneralEnum.DICT.DATA_SOURCES.getKey());

        // 字典
        Map<String, DictItemEntryVO> dictItemMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(items)) {
            items.forEach(item -> dictItemMap.put(item.getCode(), item));
        }
        // 数据封装
        maps.forEach((k, v) -> {
            HotsVO hotsVO = v.get(0);
            // 距离上次更新时间
            Long intervalTime = Math.floorDiv(Math.abs(Math.subtractExact(new Date().getTime(), hotsVO.getUpdateTime().getTime())), 1000);

            // 数据
            HotsGroupVO sourceVO = new HotsGroupVO()
                    .setSourceType(k)
                    .setSourceTypeCn(dictItemMap.get(k).getName())
                    .setLogo(dictItemMap.get(k).getIcon())
                    .setList(v)
                    .setIntervalTime(intervalTime);
            hotsGroup.add(sourceVO);
        });
        return hotsGroup;
    }

    @Override
    public HotsTrendVO getHotsTrend(Long timestamp, int day) {
        // 根据时间戳获取date
        String date = CalenderUtil.getTimeStampToFormat(timestamp, CalendarPattern.Y_M_D);

        // 根据时间戳获取time
        String time = CalenderUtil.getTimeStampToFormat(timestamp, CalendarPattern.H_M_S);

        // 指定日期统计结果
        long now = historyMapper.getHotsTrend(date, time);

        //偏移后日期
        String oldDate = CalenderUtil.getDate2Str(CalendarPattern.Y_M_D, CalenderUtil.offset(CalendarPattern.Y_M_D, date, Calendar.DAY_OF_YEAR, day));

        // 偏移日期统计结果
        long old = historyMapper.getHotsTrend(oldDate, time);

        return new HotsTrendVO().setNow(now).setOld(old);
    }
}
